$(function(){
	/*价格日历初始化*/
	var lineID = $("#lineID").val();
	var json = $("#json").val();
	var list = JSON.parse(json);
	var tt=$("#myCalendar").qn_priceCalendar({
						date:new Date(list[0].date),
						dateInfoList:list,
						css:{
							top:$("#myCalendar").position().top+25,
							left:$("#myCalendar").position().left
							},
						minDate:list[0].date,
						maxDate:list[list.length-1].date,
						dateSelected:function(date){
							var datef = dateFormat("yyyy-MM-dd",date);
							$("#myCalendar").val(datef);
							$.ajax({
								url:"/mmy/queryplan",
								type:"post",
								data:{
								"date":date,
								"lineID":lineID},
								success:function(list){
									dynamicPlanNumForm(list);
									queryPrice();
								}})
						}
					});
	/*表单校验*/
		  window.addEventListener('load', function() {
		    var forms = document.getElementsByClassName('needs-validation');
		    var validation = Array.prototype.filter.call(forms, function(form) {
		      form.addEventListener('submit', function(event) {
		        if (form.checkValidity() === false) {
		          event.preventDefault();
		          event.stopPropagation();
				  layer.msg('请输入必填项', {time: 3000});
		        }else{
					savereserveline();
				}
		        form.classList.add('was-validated');
		      }, false);
		    });
		  }, false);
})

/*价格日历方法*/
function triggerAlert(){
		tt.trigger('click','#next');
		tt.find('#next').click();
	}
	function getActive(){
			var data = $('.calendar-box').calendarGetActive();
			$("#date").attr("value",data.date);
		}
/* 动态标准、数量表单 */
function dynamicPlanNumForm(list){
	$("#standNum").html('');
	var data = list.standard;
	var table = $("#standNum");
	var tr1=$("<tr></tr>")
	var th1=$("<th>标准</th>");
	var th2=$("<th>剩余</th>");
	var th3=$("<th>单价</th>");
	var th4=$("<th>数量</th>");
	tr1.append(th1,th2,th3,th4);
	table.append(tr1);
	
	for(var i=0;i<data.length;i++){
	var tr2 = $("<tr></tr>");
	var node=data[i];
	var td1=$("<td> <label> "+node.cStandard+"</label> </td>");
	var planID=$("<input type='hidden' id='planID"+i+"' value="+node.iPlanID+" />");
	td1.append(planID);
	var td2=$("<td> <label> "+node.iNumber+"</label> </td>");
	var td3=$("<td> <label> "+node.fPrice+"</label> </td>");
	var td4=$("<td></td>");
	
	var input=$("<input id='planNum"+i+"' onchange='queryPrice()' class='form-control' type='number' required/>");
	td4.append(input);
	tr2.append(td1,td2,td3,td4);
	table.append(tr2);
	}
}
/* 预留信息提交 */
function submit(){
	
	if($("#reserveName").val() == "" || $("#reserveName").val() == null){
		var txt = "姓名为空，请输入";
		window.wxc.xcConfirm(txt, window.wxc.xcConfirm.typeEnum.error,{onOk:function(){
		$("#reserveName").focus();
						}});
		return false;
	}
	if($("#reserveTel").val() == "" || $("#reserveTel").val() == null){
		var txt = "联系方式为空，请输入";
		window.wxc.xcConfirm(txt, window.wxc.xcConfirm.typeEnum.error,{onOk:function(){
		$("#reserveTel").focus();
						}});
		return false;
	}
	if(!(/^((13[0-9])|(17[0-1,6-8])|(15[^4,\\D])|(18[0-9]))\d{8}$/.test($("#reserveTel").val()))){
		var txt = "手机号格式错误，请重新核对！";
		window.wxc.xcConfirm(txt, window.wxc.xcConfirm.typeEnum.error,{onOk:function(){
		$("#reserveTel").focus();
						}});
		return false;
	}
	if($("#myCalendar").val() == "" || $("#myCalendar").val() == null){
		var txt = "出发时间为空，请输入";
		window.wxc.xcConfirm(txt, window.wxc.xcConfirm.typeEnum.error,{onOk:function(){
		$("#reserveName").focus();
						}});
		return false;
	}
	var num = 0;
	var arrPlan = [];
	var objPlanTable = $("#standNum tr");
	for(var i=0;i<objPlanTable.length-1;i++){
		var linePlan = {};
		linePlan['planID']=$("#planID" + i).val();
		linePlan['number']=$("#planNum" + i).val();
		arrPlan.push(linePlan);
		num+=$("#planNum" + i).val();
	}
	if(num == 0){
		var txt = "数量至少选一个";
		window.wxc.xcConfirm(txt, window.wxc.xcConfirm.typeEnum.error);
		return false;
	}
	console.log(arrPlan);
	
	var arrOrder = [];
	var lineOrder = {};
	lineOrder['reserveName'] = $("#reserveName").val();
	lineOrder['reserveTel']  = $("#reserveTel").val();
	lineOrder['memo']  = $("#memo").val();
	arrOrder.push(lineOrder);
	console.log(arrOrder);
	
	var lineDate = $("#myCalendar").val();
	var lineID = $("#lineID").val();
	
	$.ajax({
		url:"/mmy/savereserveline",
		type:"post",
		data:{
			"plan":JSON.stringify(arrPlan),
			"order":JSON.stringify(arrOrder),
			"lineDate":lineDate,
			"lineID":lineID
			},
		dataType:"text",
		success:function(){
			var txt = "预留成功！";
			window.wxc.xcConfirm(txt, window.wxc.xcConfirm.typeEnum.success,{onOk:function(){
			location.href="/mmy/surroudingtourism";
			}});
		}
	})
}

/* 前台线路预留异步查询价格 */
function queryPrice(){	
	var objPlanTable = $("#standNum tr");
	var arrPlan = [];
	for(var i=0;i<objPlanTable.length-1;i++){
		var linePlan = {};
		linePlan['planID']=$("#planID" + i).val();
		linePlan['number']=$("#planNum" + i).val();
		arrPlan.push(linePlan);
	}
	console.log(arrPlan);
	
	$.ajax({
		url:"/mmy/queryprice",
		type:"post",
		data:{
			"plan":JSON.stringify(arrPlan),
			},
		dataType:"text",
		success:function(data){
			$("#reservePrice").val(data);
		}
	})
}
